﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace RecommenderSystem.misc
{
    public class Vector
    {

        private double[] elements;
        private int size;


        public Vector(int size)
        {

            if (size == 0)
            {
                this.size = Config.DEFAULT_FEATURES_NUM;
            }
            else{
                this.size = size;
            }
            
            elements = new double[this.size];

            //initialize elements 
            for (int k = 0; k < this.size; k++)
            {
                elements[k] = misc.DbUtils.randomizeNumber();
            }
        }

        public void insertValue(int index, double value)
        {
            if (index < 0 | index > this.size) { return; }
            elements[index] = value;
        }

        public double getElement(int index)
        {
            if (index < 0 | index > this.size) { return 0d; }
            return elements[index];
        }


        public int getVectorSize()
        {
            return this.size;
        }

        public static double operator * (Vector a, Vector b)
        {
            if (a.getVectorSize() != b.getVectorSize())
            {
                return 0d;
            }
            double answer = 0d;
            for (int k = 0; k < a.getVectorSize(); k++)
            {
                answer += a.getElement(k) * b.getElement(k);
            }

            return answer; 
        }

        public static Vector operator *(double scalar, Vector b)
        {
            Vector res = new Vector(b.getVectorSize());
            for (int k = 0; k < b.getVectorSize(); k++)
            {
                res.insertValue(k, b.getElement(k) * scalar);
            }

            return res;
        }

        public static Vector operator -(Vector a, Vector b)
        {
            if (a.getVectorSize() != b.getVectorSize())
            {
                return null;
            }
            Vector answer = new Vector(a.getVectorSize());
            for (int k = 0; k < b.getVectorSize(); k++)
            {
                answer.insertValue(k, a.getElement(k) - b.getElement(k));
            }

            return answer;
        }

        public static Vector operator +(Vector a, Vector b)
        {

            if (a.getVectorSize() != b.getVectorSize())
            {
                return null;
            }
            Vector answer = new Vector(a.getVectorSize());
            for (int k = 0; k < b.getVectorSize(); k++)
            {
                answer.insertValue(k, a.getElement(k) + b.getElement(k));
            }

            return answer;
        }

        public static double euclideanDistance(Vector a, Vector b)
        {
            if (a.getVectorSize() != b.getVectorSize()) { return 0d; }
            double answer = 0d;
            Vector tmpVector = a - b;
            int vectorLenght = tmpVector.getVectorSize();
            for (int k = 0; k < vectorLenght; k++)
            {
                answer += Math.Pow(tmpVector.getElement(k), 2);
            }

            return Math.Sqrt(answer);
        }

    }
}
